package cqrtplm.service.impl;

import cn.hutool.core.collection.IterUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.entity.RtCfgSerialNumberDO;
import cqrtplm.mapper.RtCfgSerialNumberMapper;
import cqrtplm.service.IRtCfgSerialNumberService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: zhengxiong.jiang
 * @createDate: 2025-11-10
 * @version: 1.0
 */
@Service
public class IRtCfgSerialNumberServiceImpl implements IRtCfgSerialNumberService {

    @Resource
    private RtCfgSerialNumberMapper rtCfgSerialNumberMapper;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;


    @Override
    public String getSerialNumber(String prefix) {
        return null;
    }

    @Override
    public String getSerialNumber(String prefix, Integer length) {
        String baseNumber = "";//按位数生成初始号
        for (Integer i = 1; i < length; i++) {
            baseNumber += "0";
        }
        baseNumber += "1";

        String nextNumber = "";
        if (StringUtils.isBlank(prefix)) {
            throw RTErrorCodeEnum.STATUS_INFO_CONTENT_IS_NULL.getException("请提供单据号前缀");
        }
        //当前日期戳
        String dateStamp = this.getNowDateStamp();
        RtCfgSerialNumberDO serialNumber = new RtCfgSerialNumberDO();
        //按前缀 获取流水号配置
        RtCfgSerialNumberDO rtCfgSerialNumberDO = new RtCfgSerialNumberDO(prefix);
        List<RtCfgSerialNumberDO> baseSerialNumber = rtCfgSerialNumberMapper.selectRtCfgSerialNumberlList(rtCfgSerialNumberDO);
        if (IterUtil.isNotEmpty(baseSerialNumber)) {//已有则获取并更新
            serialNumber = baseSerialNumber.get(0);
            nextNumber = serialNumber.getCode();//当前流水号
            //流水号+1
            nextNumber = String.valueOf(Integer.parseInt(nextNumber) + 1);
            //前缀补0补齐7位
            String str = baseNumber;
            nextNumber = str.substring(0, length - nextNumber.length()) + nextNumber;
            serialNumber.setCode(nextNumber);
            serialNumber.setCreatetime(new Date());
        } else {//没有则新增
            nextNumber = baseNumber;
            serialNumber.setPrefix(prefix);
            serialNumber.setDatestamp(dateStamp);
            serialNumber.setCode(nextNumber);
            serialNumber.setCreatetime(new Date());
        }
        serialNumber.setId(String.valueOf(snowflakeIdComponent.getInstance().nextId()));
        rtCfgSerialNumberMapper.insertRtCfgSerialNumber(serialNumber);
        //拼接
//        nextNumber = prefix + dateStamp + nextNumber;
        nextNumber = prefix + nextNumber;
        return nextNumber;
    }

    /**
     * 当前日期戳
     * @return
     */
    public static String getNowDateStamp() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String todayTime = formatter.format(new Date());
        return todayTime;
    }
}
